package cn.itsource.xhy.org.controller;

import cn.itsource.xhy.org.domain.SystemType;
import cn.itsource.xhy.org.query.SystemTypeQuery;
import cn.itsource.xhy.org.service.ISystemTypeService;
import cn.itsource.xhy.utils.AjaxResult;
import cn.itsource.xhy.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemtype")
public class SystemTypeController {

    @Autowired
    private ISystemTypeService systemTypeService;

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody SystemType systemType){
        try {
            if(systemType.getId()==null){
                systemTypeService.save(systemType);
            } else {
                systemTypeService.update(systemType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            systemTypeService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败" + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public SystemType findById(@PathVariable("id") Long id){
        try {
            return systemTypeService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping
    public List<SystemType> findAll(){
        try {
            return systemTypeService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping
    public QueryResult<SystemType> queryPage(@RequestBody SystemTypeQuery systemTypeQuery){
        return systemTypeService.queryPage(systemTypeQuery);
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            systemTypeService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败" + e.getMessage());
        }
    }

}
